#include "hal.h"
#include "mongoose_glue.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <pthread.h>

#include "dd_drv.h"

static int fd;
static double dd_value;
static int dd_chose;

extern double readVoltage();
extern double readWD();
extern double readSD();
extern double readLight();

static void blink_timer(void *arg)
{
    (void)arg;
    // mg_log("blink_timer!!!!!! dd_chose=[%d]\n", dd_chose);
    if(dd_chose == 0){
        dd_value=readWD();
        // mg_log("blink_timer:: dd_value=[%f]\n", dd_value);
        return;
    }
    if(dd_chose == 1){
        dd_value=readSD();
        return;
    }
    if(dd_chose == 2){
        dd_value=readLight();
        return;
    }
    if(dd_chose == 3){
        dd_value=readVoltage();
        return;
    }
    return;
}

void my_get_dd_show(struct dd_show *data)
{
    // data->value = 1;
    mg_log("my_get_dd_show value: %s\n", data->value);
}

void my_set_dd_show(struct dd_show *data)
{
    mg_log("my_set_dd_show value: %s\n", data->value);
    if(strstr(data->value,"0")!= NULL){
        dd_chose = 0;
        return;
    }
    if(strstr(data->value,"1")!= NULL){
        dd_chose = 1;
        return;
    }
    if(strstr(data->value,"2")!= NULL){
        dd_chose = 2;
        return;
    }
    if(strstr(data->value,"3")!= NULL){
        dd_chose = 3;
        return;
    }
    return;
}

void *dd_show_thread(void *arg)
{
    (void)arg;
    while (1)
    {
        if(dd_chose == 0||dd_chose == 1){
            int temp =(int)(dd_value*100);
            ioctl(fd, DD_LED_4_SHOW, temp%10);
            usleep(25);
            temp /= 10;
            //
            ioctl(fd, DD_LED_3_SHOW, temp%10);
            usleep(25);
            temp /= 10;
            //
            ioctl(fd, DD_LED_22_SHOW, temp%10);     
            usleep(25);
            temp /= 10;
            //
            ioctl(fd, DD_LED_1_SHOW, temp%10);     
            usleep(25);
            temp /= 10;
            continue;
        }
        else if(dd_chose == 2||3){
            int temp =(int)(dd_value);
            ioctl(fd, DD_LED_4_SHOW, temp%10);
            usleep(25);
            temp /= 10;
            //
            ioctl(fd, DD_LED_3_SHOW, temp%10);
            usleep(25);
            temp /= 10;
            //
            ioctl(fd, DD_LED_2_SHOW, temp%10);     
            usleep(25);
            temp /= 10;
            //
            ioctl(fd, DD_LED_1_SHOW, temp%10);     
            usleep(25);
            temp /= 10;
            continue;
        }

    }
    return NULL;
}

void dd_init()
{
    //
    fd = open("/dev/dd_drv", O_RDWR);
    //
    mg_timer_add(&g_mgr, 1000, MG_TIMER_REPEAT, blink_timer, NULL);
    //
    mongoose_set_http_handlers("dd_show", my_get_dd_show, my_set_dd_show);
    // 启动一个特殊的线程，刷新数显管屏幕
    pthread_t tid;
    pthread_create(&tid, NULL, (void *)dd_show_thread, NULL);
    pthread_detach(tid); // 线程分离，避免资源泄漏
}
